package com.openbravo.pos.panels.shard;

import java.util.Properties;

import com.openbravo.bean.shard.PeopleEntity;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.format.Formats;


import cn.ekuma.data.ui.swing.AbstractDAOJEditor;
import com.openbravo.pos.base.AppLocal;

import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.BorderLayout;

public class PeopleEditor extends AbstractDAOJEditor<PeopleEntity>{
	private JTextField txtFax;
	private JTextField txtPhone;
	private JTextField txtEmail;
	private JTextField txtLastName;
	private JTextField txtFirstName;
	AddressEditor addressEditor;
	private JLabel label_6;
	private JTextField textField;

	public PeopleEditor(DirtyManager dirty) {
		super(dirty);
		setPreferredSize(new Dimension(450, 390));
		initComponents();
		txtFirstName.getDocument().addDocumentListener(dirty);
        txtLastName.getDocument().addDocumentListener(dirty);
        txtEmail.getDocument().addDocumentListener(dirty);
        txtPhone.getDocument().addDocumentListener(dirty);
        txtFax.getDocument().addDocumentListener(dirty);
	}
	private void initComponents() {
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		add(panel);
		panel.setLayout(null);
		
		JLabel label = new JLabel();
		label.setText(AppLocal.getIntString("label.name"));
		label.setBounds(10, 37, 140, 15);
		panel.add(label);
		
		JLabel label_1 = new JLabel();
		label_1.setText(AppLocal.getIntString("label.lastname"));
		label_1.setBounds(10, 63, 140, 15);
		panel.add(label_1);
		
		JLabel label_2 = new JLabel();
		label_2.setText(AppLocal.getIntString("label.email"));
		label_2.setBounds(10, 89, 140, 15);
		panel.add(label_2);
		
		JLabel label_3 = new JLabel();
		label_3.setText(AppLocal.getIntString("label.phone"));
		label_3.setBounds(10, 115, 140, 15);
		panel.add(label_3);
		
		JLabel label_5 = new JLabel();
		label_5.setText(AppLocal.getIntString("label.fax"));
		label_5.setBounds(10, 143, 140, 15);
		panel.add(label_5);
		
		txtFax = new JTextField();
		txtFax.setBounds(154, 140, 270, 21);
		panel.add(txtFax);
		
		txtPhone = new JTextField();
		txtPhone.setBounds(154, 112, 270, 21);
		panel.add(txtPhone);
		
		txtEmail = new JTextField();
		txtEmail.setBounds(154, 86, 270, 21);
		panel.add(txtEmail);
		
		txtLastName = new JTextField();
		txtLastName.setBounds(154, 60, 270, 21);
		panel.add(txtLastName);
		
		txtFirstName = new JTextField();
		txtFirstName.setBounds(154, 34, 270, 21);
		panel.add(txtFirstName);
		
		label_6 = new JLabel();
		label_6.setText(AppLocal.getIntString("label.firstname"));
		label_6.setBounds(10, 11, 140, 15);
		panel.add(label_6);
		
		textField = new JTextField();
		textField.setBounds(154, 8, 270, 21);
		panel.add(textField);
		
		addressEditor=new AddressEditor(dirty);
		add(addressEditor, BorderLayout.SOUTH);
	}

	@Override
	public void refresh() {
	}

	@Override
	public PeopleEntity createNew(Properties prop) {
		
		return new PeopleEntity();
	}
	
	@Override
	public void writeValueInsert(Properties prop) {
		super.writeValueInsert(prop);
		addressEditor.writeValueInsert(prop);
	}
	@Override
	public void writeValueDelete(PeopleEntity value) {
		super.writeValueDelete(value);
		addressEditor.writeValueDelete(value);
	}
	@Override
	public void writeValueEOF() {
		super.writeValueEOF();
		addressEditor.writeValueEOF();
	}
	@Override
	public void writeValueEdit(PeopleEntity value) {
		super.writeValueEdit(value);
		addressEditor.writeValueEdit(value);
	}
	@Override
	public void rebuild(PeopleEntity obj) throws BasicException {
		addressEditor.rebuild(obj);
		obj.setFirstname((String) Formats.STRING.parseValue(txtFirstName.getText()));
		obj.setLastname((String) Formats.STRING.parseValue(txtLastName.getText())) ;
		obj.setEmail((String) Formats.STRING.parseValue(txtEmail.getText())) ;
		obj.setPhone((String) Formats.STRING.parseValue(txtPhone.getText())) ;
		obj.setFax((String) Formats.STRING.parseValue(txtFax.getText())) ;
	}

	@Override
	public void updateUI(boolean editAble, int state) {
		txtFirstName.setText(Formats.STRING.formatValue(obj.getFirstname()));
        txtLastName.setText(Formats.STRING.formatValue(obj.getLastname()));
        txtEmail.setText(Formats.STRING.formatValue(obj.getEmail()));
        txtPhone.setText(Formats.STRING.formatValue(obj.getPhone()));
        txtFax.setText(Formats.STRING.formatValue(obj.getFax()));	
        
        txtFirstName.setEnabled(editAble);
        txtLastName.setEnabled(editAble);
        txtEmail.setEnabled(editAble);
        txtPhone.setEnabled(editAble);
        txtFax.setEnabled(editAble);
	}

	@Override
	public Class getEditClass() {
		// TODO Auto-generated method stub
		return null;
	}

}
